\<?php
session_start();
require_once("lib/util.php");
require_once("lib/db.php");
require_once("lib/user.php");
connect();
$creator_id = @$_SESSION["logged_in_user"];
$user_name = query_name($creator_id);
close();
function make_incident ($category_id, $initial_rating, $incident_title, $incident_descr){
		$incident_query= "INSERT INTO incidents (creator_user_id, category_id, initial_rating, incident_title, incident_descr) ".
		"VALUES ( ?, ?, ?, ?, ?);";
		query ($incident_query, array($_SESSION["logged_in_user"], $category_id, $initial_rating, $incident_title, $incident_descr));
		}
//return true or false
function validate_incident($post) {
	//make sure title exists
	if(empty($post["title"])){
		return false;
	}
	
	//make sure category chosen
	if($post["category"]==0){
		return false;
	}

	//make sure description given
	if(empty($post["description"])){
		return false;
	}
	
	//make sure thing is rated - or try submit without choosing any
	if($post["rating"]=NULL){
		return false;
	}
	
	return true;
	//show some sort of error;
}
if(!isset($_SESSION["logged_in_user"])){
	redirect("/nitpicker/log_in.php");
	}
if($_POST) {

	if(validate_incident($_POST)) {
		//make a new nit
		connect();
		make_incident ($_POST["category"], $_POST["rating"], $_POST["title"], $_POST["description"]);
		print mysql_error();
		close ();
		redirect ("/nitpicker/index.php");
	
	} else {
			render("new_incident", array("failed" => true));
		//redirect("/nitpicker/nitform.html");
	}
} else {
	$ratings = get_ratings();
	$categories = get_categories ();
	render("new_incident", array("ratings" => $ratings, "categories" => $categories, "failed" => false, "user_name" => $user_name));
	
}
